package org.itboys.web.promotion;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mysql.promotion.SundayCoupon;
import org.itboys.mobile.entity.mysql.promotion.SundayCouponActivity;
import org.itboys.mobile.service.mysql.web.promotion.SundayPromotionService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：zq
 * 时间：2017年5月15日
 * 描述：圣代_优惠券_后台_controller
 */
@Controller
@RequestMapping("/sunday/web/promotion")
public class SundayPromotionOfCouponController extends BaseController{
    @Autowired
    private SundayPromotionService promotionService;
    /**
     * 主页
     * @param request
     * @return
     */
    @RequestMapping("/coupon/activity/index")
    public String index(HttpServletRequest request){

        return "/sunday/promotion/coupon/activity/index";
    }
    /**
     * 分页查找优惠券活动
     * @param request
     * @param response
     */
    @RequestMapping("/coupon/activity/select")
    public void coupon_activity_select(HttpServletRequest request,
                       HttpServletResponse response
    ) {
       try{
           PageResult<SundayCouponActivity> result = promotionService.selectActivity(request);
           JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
       }catch (Exception e){
           e.printStackTrace();
           AjaxUtils.renderJsonporJson(response, null,
                   ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
       }
    }
    /**
     * 分页查找优惠券
     * @param request
     * @param response
     */
    @RequestMapping("/coupon/select")
    public void coupon_select(HttpServletRequest request,
                       HttpServletResponse response
    ) {
        try{
            PageResult<SundayCoupon> result = promotionService.selectCoupon(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 新增或者修改活动
     * @param request
     * @param response
     * @param activity
     */
    @RequestMapping("/coupon/activity/save")
    public void coupon_activity_save(HttpServletRequest request,
                     HttpServletResponse response,
                     @ModelAttribute SundayCouponActivity activity){
         try{
             promotionService.saveActivity(activity);
             AjaxUtils.renderJsonporJson(response, null,
                     ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
         }catch (Exception e){
             e.printStackTrace();
             String msg = "";
             if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                 msg = e.getMessage();
             } else {
                 msg = CommonConstants.ERROMSG;
                 logger.error("/sunday/web/promotion/coupon/activity/save------" + e.getMessage());
             }
             e.printStackTrace();
             AjaxUtils.renderJsonporJson(response, null,
                     ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
         }
    }

    /**
     * 更改优惠券活动状态
     * @param request
     * @param response
     * @param id
     * @param status
     */
    @RequestMapping("/coupon/activity/change")
    public void coupon_activity_change(HttpServletRequest request,
                                     HttpServletResponse response,
                                     @RequestParam(value="id",required = true)Long id,
                                       @RequestParam(value="status",required = true)Integer status
                                     ){
        try{
            promotionService.changeCouponActivityStatus(id,status);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/promotion/coupon/activity/change------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }
    }
    /**
     * 查询单个优惠券
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/coupon/activity/findOne")
    public void coupon_activity_findOne(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value="id",required=true)Long id){
        try{
            SundayCouponActivity coupon=promotionService.findOne(id);
            AjaxUtils.renderJson(response,coupon);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
}
